package com.muk.meis.model.service;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityTransaction;

import com.muk.meis.model.bean.SchoolInfo;

public class SchoolInfoService extends BaseMEISService {
	
	public void update(SchoolInfo schoolInfoNew){
		EntityTransaction t = em.getTransaction();
		t.begin();
		SchoolInfo schoolInfoOld = em.find(SchoolInfo.class,schoolInfoNew.getPercodeCode());
		//schoolInfoOld.setDistrictId(schoolInfoNew.getDistrictId());
		schoolInfoOld.setAddress(schoolInfoNew.getAddress());
		schoolInfoOld.setBackground(schoolInfoNew.getBackground());
		schoolInfoOld.setMission(schoolInfoNew.getMission());
		schoolInfoOld.setPhilosophy(schoolInfoNew.getPhilosophy());
		schoolInfoOld.setVision(schoolInfoNew.getVision());
		em.persist(schoolInfoOld);
		t.commit();
	}
	
	public SchoolInfo findById(String perCode){
		return em.find(SchoolInfo.class, perCode);
	}
	
	public List getSchoolName(String schoolName){
		String sql = "select a.schoolName, c.districtName, b.address, a.percodeCode " +
		             "from School a, SchoolInfo b, District c " +
		             "where a.percodeCode = b.percodeCode " +
		             "      %s "+
		             "      and b.districtId = c.districtId";
		if(schoolName == null){
			sql = String.format(sql, "");
		} else {
			sql = String.format(sql, " and a.schoolName like '%"+schoolName+"%'");
		}
		return em.createQuery(sql).getResultList();
	}

	public static void main(String[] args) {
		SchoolInfoService obj = new SchoolInfoService();
		List lst = obj.getSchoolName(null);
		for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			
		}
		System.out.println(lst);
		System.out.println(((Object[])lst.get(0))[0]);
	}

}
